Skip to content

ZOOKEEPER-4391: Unable to use newer JVM memory settings as Xmx overrides - #2061

Open
smoldenhauer-ish wants to merge 2 commits into
apache:masterfrom
intershop:ZOOKEEPER-4391
Open

smoldenhauer-ish wants to merge 2 commits into
apache:masterfrom
intershop:ZOOKEEPER-4391

Conversation

@smoldenhauer-ish

@smoldenhauer-ish smoldenhauer-ish commented Sep 8, 2023 •

Copy link
Copy Markdown

allows a more flexible usage of the ZK_SERVER_HEAP/ZK_CLIENT_HEAP environment variable to specify heap related JVM settings.
It also keeps the old setting of a megabyte number for -Xmx
ZK_SERVER_HEAP="1500" sets "-Xmx1500m"
ZK_SERVER_HEAP="-XX:+UseContainerSupport -XX:MaxRAMPercentage=75"
skips the -Xmxm and adds the ZK_SERVER_HEAP as specified to the SERVER_JVM_FLAGS

same for the client settings.

@ctubbsii ctubbsii left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree this is a problem, and I think this is an adequate workaround... but it forces somebody to set a dummy value in ZK_SERVER_HEAP in order to specify different memory options directly in SERVER_JVMFLAGS. Maybe an empty string is an adequate dummy value? In any case, this seems difficult to reason about.

I suggest this alternative: If the user set JVMFLAGS, just use that. Only use the HEAP variable if the user didn't set any JVMFLAGS.

Also, I suggest shipping with a default conf/java.env or conf/zookeeper-env.sh that contains these lines. The files in the bin/ directory shouldn't be considered user configuration, and should not do any environment setup that should be the responsibility of the user to customize. Those things should occur in the conf/ directory. See, for example: https://github.com/apache/accumulo/blob/rel/2.1.3/assemble/conf/accumulo-env.sh#L93-L100

Comment thread bin/zkEnv.sh Outdated
# default heap for zookeeper server
ZK_SERVER_HEAP="${ZK_SERVER_HEAP:-1000}"
export SERVER_JVMFLAGS="-Xmx${ZK_SERVER_HEAP}m $SERVER_JVMFLAGS"
if [[ ${ZK_SERVER_HEAP} =~ ^[0-9]+.*[0-9]+$ ]]; then

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curly braces aren't needed in these square brackets. Also, I'm not sure you have the regex correct. Yours would match on any of these clearly incorrect values:

  • 00000 abc 0
  • 00 but not 0

I think what you want is "a single non-zero digit, followed by any number of additional digits", which is ^[1-9][0-9]*$

Suggested change
if [[ ${ZK_SERVER_HEAP} =~ ^[0-9]+.*[0-9]+$ ]]; then
if [[ $ZK_SERVER_HEAP =~ ^[1-9][0-9]*$ ]]; then

Comment thread bin/zkEnv.sh Outdated
if [[ ${ZK_SERVER_HEAP} =~ ^[0-9]+.*[0-9]+$ ]]; then
export SERVER_JVMFLAGS="-Xmx${ZK_SERVER_HEAP}m $SERVER_JVMFLAGS"
else
export SERVER_JVMFLAGS="${ZK_SERVER_HEAP} $SERVER_JVMFLAGS"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curly braces aren't needed here:

Suggested change
export SERVER_JVMFLAGS="${ZK_SERVER_HEAP} $SERVER_JVMFLAGS"
export SERVER_JVMFLAGS="$ZK_SERVER_HEAP $SERVER_JVMFLAGS"

Comment thread bin/zkEnv.sh Outdated
Comment on lines +147 to +151
if [[ ${ZK_CLIENT_HEAP} =~ ^[0-9]+.*[0-9]+$ ]]; then
export CLIENT_JVMFLAGS="-Xmx${ZK_CLIENT_HEAP}m $CLIENT_JVMFLAGS"
else
export CLIENT_JVMFLAGS="${ZK_CLIENT_HEAP} $CLIENT_JVMFLAGS"
fi

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative approach that is much simpler:

Suggested change
if [[ ${ZK_CLIENT_HEAP} =~ ^[0-9]+.*[0-9]+$ ]]; then
export CLIENT_JVMFLAGS="-Xmx${ZK_CLIENT_HEAP}m $CLIENT_JVMFLAGS"
else
export CLIENT_JVMFLAGS="${ZK_CLIENT_HEAP} $CLIENT_JVMFLAGS"
fi
# set memory options only if the user didn't customize the CLIENT_JVMFLAGS
if [[ -z $CLIENT_JVMFLAGS ]]; then
export CLIENT_JVMFLAGS="-Xmx${ZK_CLIENT_HEAP}m"
fi

@dukelion

Copy link
Copy Markdown
Contributor

I suggest this alternative: If the user set JVMFLAGS, just use that. Only use the HEAP variable if the user didn't set any JVMFLAGS.

SERVER_JVMFLAGS are added to JVMFLAGS here: https://github.com/apache/zookeeper/blob/master/bin/zkServer.sh#L81

@ctubbsii

ctubbsii commented Sep 18, 2025 •

Copy link
Copy Markdown
Member

I suggest this alternative: If the user set JVMFLAGS, just use that. Only use the HEAP variable if the user didn't set any JVMFLAGS.

SERVER_JVMFLAGS are added to JVMFLAGS here: https://github.com/apache/zookeeper/blob/master/bin/zkServer.sh#L81

Yes, but that's not really relevant to the discussion.

My suggestion is: if the user set CLIENT_JVMFLAGS or SERVER_JVMFLAGS, just use those as-is, and only try to infer a default heap size from the ZK_CLIENT_HEAP or ZK_SERVER_HEAP if the CLIENT_JVMFLAGS or SERVER_JVMFLAGS variables are not set by the user.

@smoldenhauer-ish

Copy link
Copy Markdown
Author

Thanks for comments & suggestions.
I omitted the curly braces where not needed and simplified the regex to check only for number. Certainly I was overthinking it a bit with ZK_SERVER_HEAP="2g -Xms512".
However I kept the default and the inclusion of ZK_SERVER_HEAP as I think there is no need to change the default behavior to have that fixed -Xmx1000m - seems to be a reasonable required default for most cases.
Yes, for removing that default, setting the ZK_SERVER_HEAP is necessary, but keeping heap and flag settings in separate vars looks quite reasonable to me.

@jprieto-temporal

Copy link
Copy Markdown

FYI, I proposed an alternative approach in #2460.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants